/**
 * @description 利用cesium GeoJsonDataSource.load方法解析要素集转成 polygon所需的参数格式
 * @param {*} features 
 * @param {*} height 
 * @returns  返回 positions,holes组成的对象数组
 */
 async function loadFeatureCollectionJsonAndParseAsPolygon(features, height = 0) {
    var promiseArray = [];
    features.forEach(feature => {
        let promise = Cesium.GeoJsonDataSource.load(feature);
        promiseArray.push(promise);
    })
    return new Promise((resolve) => {
        Promise.all(promiseArray).then((dataSources) => {
            var polygonHierarchy = []
            dataSources.forEach(dataSource => {
                let entities = dataSource.entities.values;
                let positions = [];
                let holes = [];
                entities.forEach((item) => {
                    let _positions = item.polygon.hierarchy._value.positions;
                    _positions.forEach((i) => {
                        let cartographic = Cesium.Cartographic.fromCartesian(i);
                        let lon = Cesium.Math.toDegrees(cartographic.longitude);
                        let lat = Cesium.Math.toDegrees(cartographic.latitude);
                        i = Cesium.Cartesian3.fromDegrees(lon, lat, height);
                        positions.push(i);
                    });
                    let _holes = item.polygon.hierarchy._value.holes;
                    _holes.forEach((i) => {
                        i.positions.forEach((ii, index) => {
                            let cartographic = Cesium.Cartographic.fromCartesian(ii);
                            let lon = Cesium.Math.toDegrees(cartographic.longitude);
                            let lat = Cesium.Math.toDegrees(cartographic.latitude);
                            let newPos = Cesium.Cartesian3.fromDegrees(lon, lat, height);
                            i.positions[index] = newPos;
                        });
                        holes.push(i);
                    });
                });
                polygonHierarchy.push({ positions: positions, holes: holes });
            })
            resolve(polygonHierarchy)
        });
    })
}

export default {
    loadFeatureCollectionJsonAndParseAsPolygon
}